package com.yangoxix.lovegoing.service;


import com.yangoxix.lovegoing.mapper.UserInActivityMapper;
import com.yangoxix.lovegoing.mapper.UserMapper;
import com.yangoxix.lovegoing.page.LoginPage;
import com.yangoxix.lovegoing.pojo.User;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserService {
    private final UserMapper userMapper;
    private final UserInActivityMapper userInActivityMapper;


    public UserService(UserMapper userMapper, UserInActivityMapper userInActivityMapper) {
        this.userMapper = userMapper;
        this.userInActivityMapper = userInActivityMapper;
    }

    //    查询根据用户名查询用户
    public User findByUserName(String userName) {
        User user = new User();
        User finded_User = user.selectById(userName);
        return finded_User;
    }

    public boolean isUserExisted(String username) {
        return userMapper.isUserExisted(username);
    }

    public boolean register(String username, String password, String email) {
        if (isUserExisted(username) || isUserExistedViaEmail(email))
            return false;
        else {
            User u = new User();
            u.setUsername(username);
            u.setPassword(password);
            u.setEmail(email);
            userMapper.insert(u);
            return true;
        }
    }


    public Optional<LoginPage> login(String username, String password) {
        if (!isUserExisted(username)) {
            return Optional.empty();
        } else {
            User u = userMapper.selectById(username);
            if (!u.getPassword().equals(password))
                return Optional.empty();
            else
                return Optional.of(new LoginPage(username, u.getMask()));
        }
    }

    public boolean isUserExistedViaEmail(String email) {
        return userMapper.isUserExistedViaEmail(email);
    }

    public void updateMotto(String username, String motto) {
        if (!isUserExisted(username))
            return;
        userMapper.updateMottoByUsername(username, motto);
    }


    public boolean trySendRecoverEmail(String email) {
        if (!isUserExistedViaEmail(email))
            return false;
        else {
            User u = userMapper.selectByEmail(email);
            sentRecoverEmail(u);
            return true;
        }
    }

    // TODO: placeholder here
    private void sentRecoverEmail(User u) {
    }

    public boolean tryUpdatePassword(String username, String oldPassword, String newPassword) {
        User u = userMapper.selectById(username);
        if (u.getPassword().equals(oldPassword)) {
            u.setPassword(newPassword);
            userMapper.updateById(u);
            return true;
        } else
            return false;
    }

    public Optional<String> getPortraitByUsername(String username) {
        return Optional.ofNullable(userMapper.getPortraitByUsername(username));
    }

    public Optional<String> getMottoByUsername(String username) {
        return Optional.ofNullable(userMapper.getMottoByUsername(username));
    }

    public boolean isPortraitByUsernameExist(String username) {
        return userMapper.isPortraitPresentByUsername(username);
    }
}
